var mongoose = require('mongoose');
const path = require('path');
mongoose.Promise = global.Promise;
const winston = require('../utils/winstonLogger.js');

//connect to mongodb
mongoose.set('debug',true);
mongoose.connect(
    'mongodb://localhost:27017/test',
    {useMongoClient:true}
).then(function(db){
    winston.info('DB connect success !!')
}).catch(function(err){
    winston.error('DB connect failed!!');
    winston.error(err.stack);
});

var foodModel = require('../models/Food.js');


var foods =[{
    'name': '面条',
    'price': 234,
    // food type
    'type': '主食',
    'image': 'fdsdf',
    'comments': [],
    'merchant': '59a8cea6fd6ebc162818ba15'
},{
    'name': '拉菲',
    'price': 432,
    // food type
    'type': '酒水',
    'image': 'fdsdf',
    'comments': [],
    'merchant': '59a8cea6fd6ebc162818ba15'
},{
    'name': '炸鸡腿',
    'price': 24,
    // food type
    'type': '小吃',
    'image': 'fdsdf',
    'comments': [],
    'merchant': '59a8f03738c6321d20a86908'
},{
    'name': '梅菜扣肉',
    'price': 42,
    // food type
    'type': '套餐',
    'image': 'fdsasdasdf',
    'comments': [],
    'merchant': '59a8f03738c6321d20a86909'
}]
var foodDao ={};
foodDao.add = function (foods) {
    foodModel.insertMany(foods,function(error,docs){
        if(error)
            return winston.error(error);
        else
            console.log(docs);
    })
}

foodDao.delete = function (foodId) {
    foodModel.deleteMany({_id:foodId},function(error,docs){
        if(error)
            return winston.error(error);
        else
            console.log(docs);

    })
}

var newfood = {
    '_id':"59a8f2287c07a80134efd76a",
    'name': '红烧排骨',
    'price': 46,
    // food type
    'type': '套餐',
    'image': 'fdsasdasdf',
    'comments': [],
    'merchant': '59a8f03738c6321d20a86909'
}

foodDao.update = function (newfood) {
    foodModel.updateMany({_id:newfood._id},newfood,function(error,docs){
        if(error)
            return winston.error(error);
        else
            console.log(docs);
    })
}

foodDao.query = function($n={}){
    // if($id==null){
    //     var n = {};
    // }else{
    //     var n ={_id:$id};
    // }
    foodModel.find($n,function(error,docs){
        if(error)
            return winston.error(error);
        else
            console.log(docs);
    });
}
//foodDao.add(foods);
//foodDao.delete("59a8f2287c07a80134efd76a");
//foodDao.update(newfood);
foodDao.query({type: '酒水',merchant:"12341241"});